package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    @GetMapping("/list")
    public PageDTO page(@RequestParam Integer page,
                        @RequestParam Integer size) {

       return itemService.pagelist(page,size);
    }

    @GetMapping("/{id}")
    public Item findById(@PathVariable("id") Long id) {

        return itemService.findById(id);
    }

    @PostMapping
    public void updeteItem(@RequestBody Item item) {
        log.info("刚插入的id={}",item.getId());
        itemService.save(item);
    }



    @PutMapping("/status/{id}/{status}")
    public void status(@PathVariable("id") Long id,@PathVariable("status") Integer status) {
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<> ( );
        queryWrapper.eq ( Item::getId, id );
        Item item = new Item ( );
        item.setStatus ( status );
        itemService.update ( item,queryWrapper );
        log.debug ( "刚刚停用的id={}",id );
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Long id) {
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<> ( );
        queryWrapper.eq ( Item::getId, id );
        Item item = new Item ( );
        //伪删除
        item.setStatus ( 3 );
        itemService.update ( item,queryWrapper );
        log.debug ( "刚刚删除的id={}",id );

    }

    @PutMapping
    public void update(@RequestBody Item item) {
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<> ( );
        queryWrapper.eq ( Item::getId, item.getId () );
        itemService.update ( item,queryWrapper );
        log.debug ( "刚刚修改的id={}",item.getId () );

    }



}

